<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生成区间范围的随机整数</title>
</head>

<body>


    <script>
        //方法一
        //Math.floor(Math.random()*(大数-小数)+1) +10
        //10-20
        var res = Math.floor(Math.random() * (20 - 10 + 1)) + 10
        console.log(res);

        arr = []
        for (var i = 0; i < 10000000; i++) {
            arr.push(Math.floor(Math.random() * (20 - 10 + 1)) + 10)
        }

        var obj = {}
        for (j = 0; j < arr.length; j++) {
            if (obj[arr[j]]) {
                obj[arr[j]]++
            } else {
                obj[arr[j]] = 1
            }
        }
        console.log(obj);

        //方法二 封装函数

        function fn(a, b) {
            //1.先拿到最大值和最小值
            var min = Math.min(a, b)
            var max = Math.max(a, b)

            //2.计算方式
            var res = Math.floor(Math.random() * (max - min + 1)) + min

            //把结果返回出去
            return res
        }
        fn(10, 20)
        console.log(fn(10, 20));
    </script>

</body>

</html>